//
//  ClassiFicationViewController.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/6.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "ClassiFicationViewController.h"
#import "CXClassProjectTableViewCell.h"
#import "CXOtherTableViewCell.h"
#import "CXStrategyCollectionViewCell.h"
#import "CXClassProjectTableViewCell.h"
#import "CXGiftCollectionViewCell.h"
#import "CXClickGiftViewController.h"
#import "LJFSearchViewController.h"
#import "CXProjectClickViewController.h"


static NSString *typeId = @"1";
static NSString *otherTypeId = @"2";
@interface ClassiFicationViewController () <UICollectionViewDataSource, UICollectionViewDelegate, pushView>

@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) UISegmentedControl *segament;
@end

@implementation ClassiFicationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self createNavgationSegment];
    [self createCollection];
    
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonDid)];
    self.navigationItem.rightBarButtonItem = searchItem;

}


#pragma mark - CreateCollection
- (void)createCollection {
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
    flowLaout.itemSize = CGSizeMake(self.view.width, self.view.height);
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLaout.minimumLineSpacing = 0;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:flowLaout];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.pagingEnabled = YES;
    [self.collection registerClass:[CXStrategyCollectionViewCell class] forCellWithReuseIdentifier:@"CXStrategyCollectionViewCellIdentifier"];
    [self.collection registerClass:[CXGiftCollectionViewCell class] forCellWithReuseIdentifier:@"CXGiftCollectionViewCellIdentifier"];
    [self.view addSubview:self.collection];
}



#pragma mark - CollectionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        CXStrategyCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXStrategyCollectionViewCellIdentifier" forIndexPath:indexPath];
        //block推到另外一页
        item.block = ^(void){
            CXClassFicaAllThemeViewController *allThemeVc = [[CXClassFicaAllThemeViewController alloc] init];
            [self.navigationController pushViewController:allThemeVc animated:YES];
        };
        item.pushProjectBlock = ^(NSString *newId, NSString *title) {
            CXProjectClickViewController *proClickVc = [[CXProjectClickViewController alloc] init];
            [proClickVc setGetUrlId:newId WithTypeId:otherTypeId WithTitle:title];
            [self.navigationController pushViewController:proClickVc animated:YES];
        };
        item.downPushBlock = ^(NSString *pushToUrl, NSString *navigationTitle){
            CXProjectClickViewController *proclickVc = [[CXProjectClickViewController alloc] init];
            [proclickVc setGetUrlId:pushToUrl WithTypeId:typeId WithTitle:navigationTitle];
            [self.navigationController pushViewController:proclickVc animated:YES];
        };
        return item;
    }else {
        CXGiftCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXGiftCollectionViewCellIdentifier" forIndexPath:indexPath];
        item.delegate = self;
        return item;
    }

}
#pragma mark - CollectionView Move -> SegamentValueChange
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.collection]) {
        [self.segament setSelectedSegmentIndex:scrollView.contentOffset.x / self.view.width];
    }
}

#pragma mark - CreateSegment
- (void)createNavgationSegment {
    self.segament = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"礼物"]];
    self.segament.frame = CGRectMake(0, 0, self.view.width / 2, 25);
    self.segament.selectedSegmentIndex = 0;
    self.segament.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:38 / 255.0 blue:56 / 255.0 alpha:1.0];
    self.segament.tintColor = [UIColor whiteColor];
    [self.segament addTarget:self action:@selector(segmenValueChange:) forControlEvents:UIControlEventValueChanged];
    [self.segament setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:15],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.navigationItem.titleView = self.segament;
}


#pragma mark - Segament
- (void)segmenValueChange:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        [self.collection setContentOffset:CGPointMake(0, 0) animated:NO];
    }else{
        [self.collection setContentOffset:CGPointMake(self.view.width, 0) animated:NO];
    }
}


#pragma mark - 推到下一页代理方法
- (void)pushViewWithUrlData:(NSMutableArray *)data {
    CXClickGiftViewController *clickGiftVc = [[CXClickGiftViewController alloc] init];
    clickGiftVc.getData = data;
    [self.navigationController pushViewController:clickGiftVc animated:YES];
}

#pragma mark - RightButtonDidPress

-(void)searchButtonDid{
    
    LJFSearchViewController *searchController = [[LJFSearchViewController alloc]init];
    [self.navigationController pushViewController:searchController animated:YES];
}






#pragma mark - 内存泄露
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
